package com.wzz.seckillone.config;


import com.wzz.seckillone.pojo.User;
import com.wzz.seckillone.service.UserService;
import com.wzz.seckillone.utils.MD5Util;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.util.StringUtils;

import java.util.HashSet;
import java.util.List;

/**
 * @ProjectName: jwt
 * @Package: com.wzz.jwt.config
 * @ClassName: MyRealm
 * @Author: wzz
 * @Description: shiro--Realm
 * @Date: 2022/3/17 14:00
 * @Version: 1.0
 */
@Slf4j
public class MyRealm extends AuthorizingRealm {


    @Autowired
    private UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        /*Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        //根据用户名查询 角色和权限并设置
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        HashSet<String> roles = new HashSet<>();
        List<String> roleName = roleService.getRoleByUserName(username);
        for (String s : roleName) {
            roles.add(s);
        }
        info.setRoles(roles);
        return info;*/
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       /* UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        String password = String.valueOf(token.getPassword()) ;
        User dbUser = userService.findUserByUserName(username);
        log.info(dbUser.getPassword().equals(MD5Util.md5(password+dbUser.getSalt()))+"============");
        log.info("dbUser"+dbUser);
        log.info("token 密码"+password);
        log.info("加密"+MD5Util.md5(password+dbUser.getSalt()));
        if (!dbUser.getPassword().equals(MD5Util.md5(password+dbUser.getSalt()))){
            return null;
        }
        return  new SimpleAuthenticationInfo(token.getPrincipal(), password, getName());*/
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();
        String password = String.valueOf(token.getPassword());
        User dbUser = userService.getUserById(username);
        log.info(password+"Yonghu");
        log.info("realm"+dbUser.getPassword().equals(MD5Util.md5(password+dbUser.getSalt())));
        log.info("dbUser"+dbUser.getPassword()+"==="+MD5Util.md5(password+dbUser.getSalt()));
        if (!dbUser.getPassword().equals(MD5Util.md5(password+dbUser.getSalt()))){
            return null;
        }
        return new SimpleAuthenticationInfo(token.getPrincipal(),password,getName());
    }
}
